package il.adscan.scan.json.impl;

import il.adscan.domain.Advertisement;
import il.adscan.domain.AdvertisementFilter;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class Yad2Scanner extends BaseScanner {


    @Override
    public String getSiteURL() {
        return "http://www.yad2.co.il/Nadlan/sales.php";
    }

    @Override
    protected Set<Advertisement> scrapPageData(Document doc) throws IOException {
        Elements elements = doc.select("#gridWrapper1 [onclick*=openGridTr]");
        Set<Advertisement> advertisementList = new HashSet<>();
        int i = 0;
        for (Element element : elements) {
            if (advertisementList.size() >= numberOfItemsToScan && numberOfItemsToScan != -1) {
                break;
            }
            String linkAttr = element.attr("onclick");
            String link = linkAttr.substring(linkAttr.indexOf("http"), linkAttr.indexOf("rnd") + 7);
            System.out.println(i + link);
//            Advertisement advertisement = getAdvertisementFromLink(link);
//            advertisement.setTimeAdded(element.select(".reddate").html());
//            advertisementList.add(advertisement);
            i++;
        }
        return advertisementList;
    }

    @Override
    protected Document getPage(int pageNumber, String baseURI) throws IOException {
        return getHTMLPage("http://www.yad2.co.il/Nadlan/sales.php");
    }

    @Override
    protected boolean hasNextPage(Document htmlPage) {
        return false;
    }

    @Override
    protected boolean isProcessCurrentPage(Document htmlPage) {
        return true;
    }

    @Override
    protected Document applyFilters(Document htmlPage, AdvertisementFilter filter) {
        return htmlPage;
    }
}
